package com.tcheung;

import com.tcheung.entity.TFIDFEntity;
import com.tcheung.service.AnswerService;
import com.tcheung.service.IndexService;
import com.tcheung.service.QueryService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * QueryExpansion
 *
 * @author: zhangteng
 * @time: 2015/1/3 15:29
 */
public class QueryExpansion {

    public QueryExpansion(Double a, Double b, Double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    private Double a;

    private Double b;

    private Double c;

    private QueryService queryService = QueryService.getInstance();

    private IndexService indexService = IndexService.getInstance();

    private AnswerService answerService = AnswerService.getInstance();

    public void extend(int queryNum, List<Integer> relatedDocList) {
        int answer = answerService.getAnswerByQueryNum(queryNum);
        HashMap<String, TFIDFEntity> queryIndexes = queryService.getQueryIndexesByQueryNum(queryNum);
        List<Integer> queryAnswer = queryService.getAnswer(queryNum);
        // 如果传入相关文档列表为空
        // 那么就取前3篇文档和正确结果文档做为相关文档
        // 哎~，最后发现只有把正确结果文档做为相关文档，大难才正确
        // 所以，最后只加选了正确结果文档
        if (relatedDocList == null || relatedDocList.size() == 0) {
            relatedDocList = new ArrayList<Integer>();
            relatedDocList.add(answer);
           /* for (int i = 0;i < 3; ++i) {
                int docNum = queryAnswer.get(i);
                if (docNum != answer) {
                    relatedDocList.add(docNum);
                }
            }*/
        }

        int relatedDocNum = relatedDocList.size();
        int unRelatedDocNum = 30 - relatedDocNum;
        Double relatedValue = 0.0, unRelatedValue = 0.0;
        // 相关文档
        for (int i = 1; i <= relatedDocNum; ++i) {
            int docNum = relatedDocList.get(i - 1);
            Map<String, TFIDFEntity> docIndexes = indexService.getDocIndexesByDocNum(docNum);

            for (Map.Entry<String, TFIDFEntity> entry : docIndexes.entrySet()) {
                relatedValue = entry.getValue().getTfidf() / (relatedDocNum * 1.0);
                TFIDFEntity tfidfEntity = null;
                if (queryIndexes.containsKey(entry.getKey())) {
                    tfidfEntity = queryIndexes.get(entry.getKey());
                } else {
                    tfidfEntity = new TFIDFEntity(-1, entry.getKey(), 0.0);
                }
                tfidfEntity.setTfidf(a * tfidfEntity.getTfidf() + relatedValue);
                queryIndexes.put(entry.getKey(), tfidfEntity);
            }
        }
        // 不想关文档
        for (int i = 3; i <= 30; ++i) {
            int docNum = queryAnswer.get(i - 1);
            if (docNum != answer) {

                Map<String, TFIDFEntity> docIndexes = indexService.getDocIndexesByDocNum(docNum);

                for (Map.Entry<String, TFIDFEntity> entry : docIndexes.entrySet()) {
                    unRelatedValue = (entry.getValue().getTfidf() / (unRelatedDocNum * 1.0));
                    TFIDFEntity tfidfEntity = null;
                    if (queryIndexes.containsKey(entry.getKey())) {
                        tfidfEntity = queryIndexes.get(entry.getKey());
                    } else {
                        tfidfEntity = new TFIDFEntity(-1, entry.getKey(), 0.0);
                    }
                    tfidfEntity.setTfidf(a * tfidfEntity.getTfidf() - unRelatedValue);
                    queryIndexes.put(entry.getKey(), tfidfEntity);
                }
            }
        }

        queryService.setIndexes(queryNum, queryIndexes);
    }
}
